package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.MyMovement;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void addContacts(Long userId, Long friendId) {
        //1.我加她
        //1.1 判断是否有好友关系
        Query query = new Query(
                Criteria.where("userId").is(userId)
                .and("friendId").is(friendId)
        );
        boolean exists = mongoTemplate.exists(query, Friend.class);
        if(exists ==false){ //没有加过他  建立好友关系
            //1.2 建立好友关系
            Friend friend = new Friend();
            friend.setUserId(userId);//我的id
            friend.setFriendId( friendId );//对方id
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);//加好友

            //1.3 动态推荐  查看对方的所有动态
            //将对方(我的动态) 全部查询出来
            List<MyMovement> myMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + friendId);
            //1.4 添加
            if(CollectionUtil.isNotEmpty(myMovementList)){
                for (MyMovement myMovement : myMovementList) {
                    //将对方的("我的动态") 添加到 我的朋友圈中
                    //创建我的朋友圈动态
                    FriendMovement friendMovement  = new FriendMovement();
                    friendMovement.setUserId(friendId);//好友的id
                    friendMovement.setPublishId( myMovement.getPublishId() ); //发的朋友圈id
                    friendMovement.setCreated( myMovement.getCreated() ); //当时朋友发送动态的时间
                    //添加到我的朋友圈动态中
                    mongoTemplate.save(friendMovement ,  ConstantUtil.MOVEMENT_FRIEND+userId);
                }
            }
        }


        //2.她加我
        //1.1 判断是否有好友关系
        Query query2 = new Query(
                Criteria.where("userId").is(friendId)
                        .and("friendId").is(userId)
        );
        boolean exists2 = mongoTemplate.exists(query2, Friend.class);
        if(exists2 ==false){ //没有加过他  建立好友关系
            //1.2 建立好友关系
            Friend friend = new Friend();
            friend.setUserId(friendId  );//我的id
            friend.setFriendId( userId);//对方id
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);//加好友

            //1.3 动态推荐  查看对方的所有动态
            //将对方(我的动态) 全部查询出来
            List<MyMovement> myMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);
            //1.4 添加
            if(CollectionUtil.isNotEmpty(myMovementList)){
                for (MyMovement myMovement : myMovementList) {
                    //将对方的("我的动态") 添加到 我的朋友圈中
                    //创建我的朋友圈动态
                    FriendMovement friendMovement  = new FriendMovement();
                    friendMovement.setUserId(userId);//好友的id
                    friendMovement.setPublishId( myMovement.getPublishId() ); //发的朋友圈id
                    friendMovement.setCreated( myMovement.getCreated() ); //当时朋友发送动态的时间
                    //添加到我的朋友圈动态中
                    mongoTemplate.save(friendMovement ,  ConstantUtil.MOVEMENT_FRIEND+friendId);
                }
            }
        }






    }

    //根据用户的id查询好友的列表
    @Override
    public PageBeanVo findFriendByPage(Long userId, Integer pageNum, Integer pageSize) {
        int startIndex = (pageNum-1)*pageSize;
        Query query = new Query(
                Criteria.where("userId").is(userId) //我的好友列表
        ).skip(startIndex).limit(pageSize);

        //查询集合
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        //查询总记录数据
        long count = mongoTemplate.count(query, Friend.class);
        return new PageBeanVo(pageNum ,pageSize , count , friends);
    }
}
